package com.laomou.poker.player;

import java.util.ArrayList;
import java.util.List;

import com.laomou.poker.Poker;
import com.laomou.poker.PokerGroup;
import com.laomou.poker.rule.IPokerRule;
import com.laomou.poker.rule.PokerRuleType;
import com.laomou.poker.table.Table;

public abstract class AbsPlayer {
    /**
     * 角色手上的牌
     */
    private List<Poker> mPlayerPoker = new ArrayList<Poker>();
    private String mName = "Player";
    private IPokerRule mRule;
    private Table mTable;
    private PlayerType mPlayerType;

    /**
     * 是否抢头家
     *
     * @return true 抢 false 不抢
     */
    public abstract boolean isRobFirst();

    /**
     * 发牌后调用，用于角色分析当前手牌
     */
    public abstract void analysePoker();

    /**
     * 获取角色首发出牌
     *
     * @return 出牌
     */
    public abstract PokerGroup getFirstDiscardPoker();

    /**
     * 获取角色出牌
     *
     * @param type  PlayerType 当前出牌玩家类型
     * @param poker PokerGroup 当前出牌
     * @return 出牌
     */
    public abstract PokerGroup getDiscardPoker(PlayerType type, PokerGroup pg);

    /**
     * 添加扑克牌
     *
     * @param pn Poker
     */
    public void addPoker(PokerGroup pg) {
        for (Poker p : pg.get()) {
            mPlayerPoker.add(p);
        }
    }

    /**
     * 获取角色手上的牌
     */
    public List<Poker> getPlayerPoker() {
        return mPlayerPoker;
    }

    /**
     * 加入桌子
     *
     * @param table
     */
    public void addTable(Table table) {
        mTable = table;
    }

    /**
     * 设置角色名称
     */
    public final void setPlayerName(String name) {
        mName = name;
    }

    /**
     * 获取角色名称
     */
    public final String getPlayerName() {
        return mName;
    }

    /**
     * 设置扑克牌规则
     *
     * @param rule PokerRuleType
     * @see PokerRuleType
     */
    public final void setPokerRule(IPokerRule rule) {
        mRule = rule;
    }

    /**
     * 设置角色类型
     *
     * @param type PlayerType
     * @see PlayerType
     */
    public final void setPlayerType(PlayerType type) {
        mPlayerType = type;
    }

    /**
     * 获取角色类型
     */
    public PlayerType getPlayerType() {
        return mPlayerType;
    }

    /**
     * 当出牌后删除角色手上的牌
     *
     * @param pg PokerGroup
     */
    public final void removePoker(PokerGroup pg) {
        for (Poker p : pg.get()) {
            mPlayerPoker.remove(p);
        }
    }

    /**
     * 获取桌子上的牌
     */
    public List<Poker> getTablePoker() {
        return mTable.getTableAllPoker();
    }

    /**
     * 角色手上没有牌为赢
     *
     * @return true 赢 false 没有赢
     */
    public final boolean win() {
        return mPlayerPoker.size() <= 0;
    }

    /**
     * 判断参数(lp)的牌是否大于参数(rp)的牌
     *
     * @param lp PokerGroup
     * @param rp PokerGroup
     * @return true 大于 false 小于
     */
    public final boolean isGreater(PokerGroup lp, PokerGroup rp) {
        return mRule.isGreater(lp, rp);
    }

    /**
     * 判断是否大于参数(poker)的牌
     *
     * @param poker
     * @param list
     * @return true 有 false 没有
     */
    public final boolean getGreaterPokerGroup(final PokerGroup pg, List<PokerGroup> list) {
        PokerGroup playerPg = new PokerGroup();
        playerPg.addPokers(mPlayerPoker);
        return mRule.hasBigPoker(playerPg, pg, list);
    }

    /**
     * 判断是否符合规则
     *
     * @param pg
     * @return
     */
    public final boolean isRule(PokerGroup pg) {
        return mRule.isRule(pg);
    }
}
